package com.app.service;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;

import org.apache.http.HttpConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.HTTP;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;



import android.graphics.Bitmap;

import com.app.activity.R;
import com.app.data.InnItem;

public class HotelService extends BaseService {
	

	int errorcode = 0;
	String urlString = url_base + "/api/hotel.php?htype=2";
	public HotelService(HttpCallBack callBack) {
		super(callBack);
	}

	@Override
	protected String doInBackground(String... params) 
	{
		return connectUrl(urlString);
	}
	
	@Override
    protected void onCancelled() {
        super.onCancelled();
    }

    @Override
    protected void onPostExecute(String result) 
    {
        // 返回HTML页面的内容
    	
       System.out.println(">>>>>>res :"+result);
       try {
    	   ArrayList<InnItem> list = new ArrayList<InnItem>();
    	   JSONObject object = new JSONObject(result);
    	   
    	   JSONArray array = object.getJSONArray("data");
    	   int len = array.length();
    	   for (int i = 0; i < len ; i++) {
    		   //InnItem item = InnItem.item(name, site, drawId, money)
    		   JSONObject obj = (JSONObject) array.get(i);
    		   String id = (String)obj.get("aid");
    		   String fx = (String)obj.get("fx");
    		   String title = (String)obj.get("title");
    		   String fxs[] = fx.split("\\|");
    		   String imgUrl = url_base +  obj.getString("litpic");
    		   Bitmap bitmap = BitmapUtil.getNetBitmap(imgUrl);
    		   String money = fxs[6];
    		   
    		   String htmlContent =(String)obj.get("jj");
    		   System.out.println(">>>>>>add item id :" + id + ",title :" + title +",money:" + money);
    		   InnItem item = InnItem.item("酒店编号" + id, title, R.drawable.inn_icon, "￥" + money + "起");
    		   item.setIconbitmap(bitmap);
    		   item.setHtmlContent(htmlContent);
    		   list.add(item);
    	   }
    	   
    	   callback.hotelFinishLoad(0, list);
       } catch (JSONException e) {
    	   e.printStackTrace();
    	   callback.hotelFinishLoad(-1, new ArrayList<InnItem>());
       } 
    }

	
}
